<?php
 /*----------------------------   试验 存储过程  --------------------------------------------*/
function wk_get_kj_game28($No,$zj_a,$zj_b,$zj_c,$zj_result,$strkjNum){
	global $db;
	$v_GameType = 3;
	$p_numa = $zj_a;
	$p_numb = $zj_b;
	$p_numc = $zj_c;
	$p_No = $No;

	$sql = "SELECT tzpoints,DATE_FORMAT(kgtime,'%Y-%m-%d') FROM game28 WHERE id = $No AND kj = 0";
			// INTO v_tzPoints,v_opentime;
	$result = $db->query($sql);
	$users = $db->fetch_row($result);
	$v_tzPoints = $users['0'];
	$v_opentime = $users['1'];
 
	// if(empty($v_tzPoints) || $v_tzPoints == 'null'){
		// return array('v_retmsg'=>'该期已开奖','v_result'=>1);
	// }elseif($v_tzPoints == 0){
		// return array('v_retmsg'=>'游戏已停止','v_result'=>2);
	// }
 
// -- wk:开奖  函数
// --  新建统一函数  投注单号    p_No
// --  新建统一函数  需要的参数  p_numa p_numb p_numc  中奖的号码
// --  新建统一函数  投注类型    p_kjnum  中奖之和
// --  新建统一函数  投注类型    p_type  大小单双之类的
	$v_num = 0;/* 中奖总人数 */
	for($i=1;$i<=17;$i++){
		// $sql = "SELECT count(*) FROM game28_kg_users_tz WHERE `No` = $No AND tznum = $i AND usertype = 0";
		$sql = "SELECT count(*) FROM game28_kg_users_tz WHERE `No` = $No AND tznum = $i ";
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_tz_num = $users['0'];
		if($v_tz_num > 0){ 
			$v_num += get_kj($No,$zj_a,$zj_b,$zj_c,$zj_result,$i);
			$v_tz_num = 0;	
		}
	}
	$v_ChangeRewardCount = $v_num;

	// --  每天实时输赢
	$sql ="	UPDATE game_day_static AS a,
		(
        SELECT IFNULL(SUM(hdpoints),0) AS sumhdpoints,IFNULL(SUM(tzpoints),0) AS sumtzpoints,uid
			FROM  game28_kg_users_tz WHERE `No` = $p_No AND usertype = 0 GROUP BY uid
		) AS b
		SET a.points = a.points + b.sumhdpoints - b.sumtzpoints,a.tzpoints=a.tzpoints+b.sumtzpoints
		WHERE a.uid = b.uid AND a.`time`= $v_opentime AND a.kindid=$v_GameType";
	$db->query($sql);	
		
		
	$sql ="	SELECT IFNULL(SUM(hdpoints),0) AS sumhdpoints,IFNULL(SUM(tzpoints),0) AS sumtzpoints
			FROM game28_kg_users_tz
			WHERE `NO` = $p_No AND usertype = 0";
	$result = $db->query($sql);
	$users = $db->fetch_row($result);
	$v_UserWin = $users['0'];
	$v_usrtzPoints = $users['1'];


	 
	// $v_ingame_num = POWER(2,$v_GameType);
	$v_ingame_num = 2;
	  
// -- 更新冻结/游戏状态
	$sql = "UPDATE users AS a,
	(
	SELECT uid,SUM(tzpoints) AS points FROM game28_kg_users_tz WHERE `NO` = $p_No AND usertype = 0 GROUP BY uid
	) b
	SET a.lock_points = a.lock_points - b.points,a.ingame = (a.ingame ^ $v_ingame_num)
	WHERE a.id = b.uid AND a.usertype = 0";
	$db->query($sql);	
	// -- 刷新在线状态
	$sql = "UPDATE users AS a,
	(
	SELECT uid FROM game28_kg_users_tz WHERE `NO` > $p_NO AND usertype = 0 GROUP BY uid
	) b
	SET a.ingame = (a.ingame | $v_ingame_num)
	WHERE a.id = b.uid AND a.usertype = 0";
		
		
		
		
		// -- 游戏扣税
		$v_gameSamples = 50;
		$v_gameTax =  CEIL($v_usrtzPoints * $v_gameSamples/10000);
		// -- 记录中央银行
		// -- SELECT score FROM centerbank WHERE bankIdx IN(14,15) FOR UPDATE;
		if($v_gameTax > 0){
			$sql = "UPDATE centerbank SET score = score + $v_gameTax WHERE bankIdx = 14";
			$db->query($sql);
		}
		
		$sql = "UPDATE centerbank SET score = score - ($v_UserWin - $v_usrtzPoints) - $v_gameTax  WHERE bankIdx = 15";
		$db->query($sql);
		
		
		
		 
		// -- 记录每天统计
		if($db->query("SELECT 1 FROM webtj WHERE `time` = CURDATE()")){
			$sql = "UPDATE webtj SET game28 = game28 + ($v_UserWin - $v_usrtzPoints),gametax = gametax + $v_gameTax
				WHERE `time` = CURDATE()";
			$db->query($sql);
		}else{
			$sql = "INSERT INTO webtj(`time`,game28,gametax) VALUES(CURDATE(),$v_UserWin - $v_usrtzPoints,$v_gameTax)";
			$db->query($sql);
		}
		
		$sql =" SELECT COUNT(uid),IFNULL(SUM(hdpoints),0) FROM game28_kg_users_tz WHERE `No` = $p_No AND hdpoints > 0
		";
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_RewardrCount = $users['0'];
		$v_RewardsumPoints = $users['1'];
		 
 
		$sql ="SELECT game_std_odds FROM game_config WHERE game_type = $v_GameType";
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_ReardOdds_str = $users['0'];
	
		$kgjg = $p_numa .'|'. $p_numb.'|'. $p_numc.'|'. $zj_result;
		$take_time_remark = $v_RewardOdds .'|'. $v_ChangeRewardCount.'|'. $v_RewardsumPoints;
		$sql = "UPDATE game28 SET kgjg = '$kgjg' ,take_time_remark = '$take_time_remark',
			kj = 1,zjrnum = $v_RewardrCount,zjpl = $v_ReardOdds_str,kgNo = $strkjNum,game_tax = $v_gameTax,user_tzpoints = $v_usrtzPoints,user_winpoints = $v_UserWin
			WHERE id = $p_No";
		$db->query($sql);
		
		 
		// -- 记录分明细
		$sql = "INSERT INTO user_score_changelog(gametype,gameno,uid,points,back,lock_points,experience,change_points,thetime,remark)
			SELECT v_GameType,p_No,a.id,a.points,a.back,a.lock_points,a.experience,b.sumhdpoints,NOW(),'开奖后'
			FROM users a,
			(
            SELECT uid,SUM(hdpoints) AS sumhdpoints FROM game28_kg_users_tz WHERE `No` = $p_No AND usertype = 0 GROUP BY uid
			) b
			WHERE a.id = b.uid AND a.usertype = 0";
			

		// -- 删除预投注记录
		$sql = "DELETE FROM game28_kg_users_tz WHERE `No` = $p_No";
		$db->query($sql);	
		
		
		$sql = "CALL web_gameno_addpatch('gamebj',$v_GameType,0,10)";
		$db->query($sql);	
		 
		$NextNo = $p_No + 1;
		//蛋蛋28自动投注
		$sql = "call web_tz_game28_auto_new({$p_No},{$NextNo})"; 
		$arrT = $db->Mysqli_Multi_Query($sql);
		return array('v_retmsg'=>'开奖成功','v_result'=>0);
}
	
	

function get_kj($p_No,$zj_a,$zj_b,$zj_c,$p_kjnum,$p_type){
	global $db;
	$v_GameType = 3;
	$v_num = 0;
	// -- 抽税  默认是 50   v_gameSamples
	$sql = "SELECT game_go_samples FROM game_config WHERE game_type = $v_GameType";
	$result = $db->query($sql);
	$users = $db->fetch_row($result);
	$v_gameSamples = $users['0'];

	
	// -- wk 获取 后台设置的赔率
	$sql = "SELECT game_std_odds FROM game_config WHERE game_type = $v_GameType";
	$result = $db->query($sql);
	$users = $db->fetch_row($result);
	$v_ReardOdds_str = explode('|',$users['0']);
	 // -- 最终的中奖赔率
	$v_RewardOdds = $v_ReardOdds_str[$p_type] * 1.00;
	$v_RewardNum = $p_kjnum;
	
	/* 验证类型等于 17  */
	if($p_type == 17){
		// --  更新中奖   变量 类型等于 17 并且填写的数字等于开奖数字
		$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
		SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
		WHERE t.`No` = $p_No AND t.tznum = $p_type AND  numbers = $v_RewardNum";
		$db->query($sql);
		// -- 批量更新中奖 增加奖励 
		$sql = "UPDATE users AS a,
			(
			SELECT uid,hdpoints
				FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  numbers = $v_RewardNum AND usertype = 0
			) AS b
		SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
		WHERE a.id = b.uid AND a.usertype = 0";
		$db->query($sql);
		$v_num = $db->affected_rows();
		 
		$sql = "UPDATE game_static AS a,
			(
			SELECT uid,hdpoints
				FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  numbers = $v_RewardNum AND usertype = 0
			) AS b
		SET a.points = a.points + b.hdpoints
		WHERE a.uid = b.uid AND a.typeid = $v_GameType";
		$db->query($sql);
	}
	
	 // 2 3 4 5 8 12 13 14 15 16   一个类型                STAET
	if($p_type == 2 || $p_type == 3 || $p_type == 4 || $p_type == 5 || $p_type == 8 || $p_type == 12 || $p_type == 13 || $p_type == 14 || $p_type == 15 || $p_type == 16){
		$type = 'type'.$p_type;
		$sql = "SELECT $type FROM game_config WHERE game_type = $v_GameType";	
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_InNum_str = $users['0'];
		if($v_InNum_str != ''){
		
			// --  更新中奖    
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str)";
			$db->query($sql);
			// -- 批量更新中奖 增加奖励 
			$sql = "UPDATE users AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
			WHERE a.id = b.uid AND a.usertype = 0";
			$db->query($sql);
			$v_num = $db->affected_rows();
		 
			$sql = "UPDATE game_static AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points + b.hdpoints
			WHERE a.uid = b.uid AND a.typeid = $v_GameType";
			$db->query($sql);

		
		}
		
		
		
		
	}
	
	
	
	// --  会跳出的 类型  大 1 小单 6 大双 7
	if($p_type == 1 || $p_type == 6 || $p_type == 7){
		$type = 'type'.$p_type;
		$yu_type = 'yu_type'.$p_type;
		$cx_type = 'cx_type'.$p_type;
		
		
		$sql = "SELECT $type ,$yu_type, $cx_type ,cx_pv FROM game_config WHERE game_type = $v_GameType";	
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_InNum_str = $users['0'];
		$yu_type = $users['1'];
		$cx_type = $users['2'];
		$cx_pv = $users['3'];
		$instr = '';
		if(empty($yu_type) || !$yu_type){
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
				SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
				WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str)";
			$db->query($sql);
			
		}else{
			
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
				SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
				WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum IN ($cx_type)";
			$db->query($sql);
			
			// -- 选择的是大  并且等于14 并且投注金额大于 1000  是1.85倍  这里是两个变量
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum = $yu_type AND tzpoints < 1000";
			$db->query($sql);
		 // -- 变量  需要设置变量    1000  和  1.85  两个需要设置
			// SET v_RewardOdds = 1.85 * 1.00; -- 最终的中奖赔率
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum = $yu_type AND tzpoints > 1000";
			$db->query($sql);
			
			
		}

		 // -- 批量更新中奖
		 $sql = "UPDATE users AS a,
			(
			SELECT uid,hdpoints
				FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
			) AS b
		SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
		WHERE a.id = b.uid AND a.usertype = 0" ;
		$db->query($sql);
		$v_num = $db->affected_rows();
	 
		$sql = "UPDATE game_static AS a,
			(
			SELECT uid,hdpoints
				FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str)  AND usertype = 0
			) AS b
		SET a.points = a.points + b.hdpoints
		WHERE a.uid = b.uid AND a.typeid = $v_GameType";
		$db->query($sql); 
 
	
	}
	
	
	//顺子 9
	if($p_type == 9){
		$type = 'type'.$p_type;
		$sql = "SELECT $type FROM game_config WHERE game_type = $v_GameType";	
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_InNum_str = $users['0'];
		$arr = array($zj_a,$zj_b,$zj_c);
		sort($arr);
		$v_RewardNum = implode('',$arr);
		if($v_InNum_str != ''){
		
			// --  更新中奖    
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str)";
			$db->query($sql);
			// -- 批量更新中奖 增加奖励 
			$sql = "UPDATE users AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
			WHERE a.id = b.uid AND a.usertype = 0";
			$db->query($sql);
			$v_num = $db->affected_rows();
		 
			$sql = "UPDATE game_static AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points + b.hdpoints
			WHERE a.uid = b.uid AND a.typeid = $v_GameType";
			$db->query($sql);
		}
	
	}
	
	
	//对子 10
	if($p_type == 10){
		$type = 'type'.$p_type;
		$sql = "SELECT $type FROM game_config WHERE game_type = $v_GameType";	
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_InNum_str = $users['0'];
		
		/* 取出来 成对子的数字 做验证 */
		$arr = array($zj_a,$zj_b,$zj_c);
		sort($arr);
		$dui = '';
		if($arr['0'] == $arr['1']){
			$dui = $arr['0'].$arr['0'];	
		}elseif($arr['1'] == $arr['2']){
			$dui = $arr['1'].$arr['1'];	
		}elseif($arr['0'] == $arr['2']){
			$dui = $arr['0'].$arr['0'];	
		}
		
		if($v_InNum_str != '' && $dui != ''){
		
			// --  更新中奖    
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $dui IN ($v_InNum_str)";
			$db->query($sql);
			// -- 批量更新中奖 增加奖励 
			$sql = "UPDATE users AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $dui IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
			WHERE a.id = b.uid AND a.usertype = 0";
			$db->query($sql);
			$v_num = $db->affected_rows();
		 
			$sql = "UPDATE game_static AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $dui IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points + b.hdpoints
			WHERE a.uid = b.uid AND a.typeid = $v_GameType";
			$db->query($sql);
		}
		
	}
	
	
	//豹子 11
	if($p_type == 11){
		$type = 'type'.$p_type;
		$sql = "SELECT $type FROM game_config WHERE game_type = $v_GameType";	
		$result = $db->query($sql);
		$users = $db->fetch_row($result);
		$v_InNum_str = $users['0'];
		$arr = array($zj_a,$zj_b,$zj_c);
		$v_RewardNum = implode('',$arr);
		if($v_InNum_str != ''){
		
			// --  更新中奖    
			$sql = "UPDATE game28_kg_users_tz t LEFT JOIN users u ON u.id=t.uid
			SET t.hdpoints = FLOOR((t.tzpoints - (t.tzpoints / 10000 * $v_gameSamples) ) * ($v_RewardOdds*(10000 - u.kf) / 10000))
			WHERE t.`No` = $p_No AND t.tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str)";
			$db->query($sql);
			// -- 批量更新中奖 增加奖励 
			$sql = "UPDATE users AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points +  FLOOR(b.hdpoints * (10000 - a.kf) / 10000)
			WHERE a.id = b.uid AND a.usertype = 0";
			$db->query($sql);
			$v_num = $db->affected_rows();
		 
			$sql = "UPDATE game_static AS a,
				(
				SELECT uid,hdpoints
					FROM game28_kg_users_tz WHERE `No` = $p_No AND tznum = $p_type AND  $v_RewardNum IN ($v_InNum_str) AND usertype = 0
				) AS b
			SET a.points = a.points + b.hdpoints
			WHERE a.uid = b.uid AND a.typeid = $v_GameType";
			$db->query($sql);
		}
	}
	
	
	
		 
	 
	$sql ="	INSERT INTO game28_users_tz(uid,`NO`,tznum,tzpoints,zjpoints,`TIME`,points,hdpoints,zjpl)
			SELECT t.uid,t.`NO`,GROUP_CONCAT(t.tznum ORDER BY t.id ASC SEPARATOR '|'),
				GROUP_CONCAT(t.tzpoints ORDER BY t.id ASC SEPARATOR '|'),
				GROUP_CONCAT(t.hdpoints ORDER BY t.id ASC SEPARATOR '|'),
				t.`TIME`,SUM(t.tzpoints),SUM(t.hdpoints) ,($v_RewardOdds*(10000 - u.kf) / 10000)
			FROM game28_kg_users_tz
			t LEFT JOIN users u ON u.id=t.uid
			WHERE t.`NO` = $p_No AND t.usertype = 0 GROUP BY t.uid";
	$db->query($sql);	
	return $v_num;
}
	
	 
?> 